﻿@*
    // Copyright (c) Alex Reich.
    // Licensed under the CC BY 4.0 License.
*@
@using RulesEngineEditor.Models
@inject RulesEngineEditor.Services.WorkflowService WorkflowService

@if (Input != null)
{
    <sp_grid_kvp>
        <div>Input Name</div>
    </sp_grid_kvp>
    <EditForm EditContext="@EditContext">
        <DataAnnotationsValidator />
        <ValidationSummary />
        <sp_grid_workflow>
            <div>
                <InputText title="Name" class="form-control" @bind-Value="@Input.InputRuleName" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" />
            </div>
            <div>
                <button title="Delete Input" @onclick="@(e => DeleteInput())" class="btn btn-secondary">
                    <span class="oi oi-trash"></span>
                </button>
            </div>
        </sp_grid_workflow>
        <h5>Parameters</h5>
        <button class="reeditor_button" title="Add Input Parameter" @onclick="AddParameter">Add</button>
        <button class="reeditor_button @IsPressed(sort, "button_depressed")" @onclick="(() => sort = !sort)">Arrange</button>
        <sp_grid_kvp>
            <div>Input Param Name</div>
            <div>Value</div>
        </sp_grid_kvp>
    </EditForm>
    @if (sort)
    {
        <Dropzone Items="Input.Parameters">
            <div class="rules_arrange">
                <InputParamEditor InputParam="@context" InputParamDelete="DeleteInputParam" />
            </div>
        </Dropzone>
    }
    else
    {
        @foreach (var context in Input.Parameters)
        {
            <InputParamEditor InputParam="@context" InputParamDelete="DeleteInputParam" />
        }
    }
}
@code {
    public bool sort;

    public string IsPressed(bool state, string suffix)
    {
        return state ? "reeditor_" + suffix : "";
    }

    [Parameter]
    public InputRuleParameter Input { get; set; }

    [Parameter]
    public EventCallback<InputRuleParameter> InputDeleted { get; set; }

    private EditContext EditContext;

    protected override void OnInitialized()
    {
        EditContext = new EditContext(Input);
        EditContext.OnFieldChanged += EditContext_OnFieldChanged;

        base.OnInitialized();
    }

    public override async Task SetParametersAsync(ParameterView parameters)
    {
        await base.SetParametersAsync(parameters);
    }

    // Note: The OnFieldChanged event is raised for each field in the model
    private void EditContext_OnFieldChanged(object sender, FieldChangedEventArgs e)
    {
        WorkflowService.InputUpdate();
    }

    private void DeleteInputParam(InputParameter inputParameter)
    {
        Input.Parameters.Remove(inputParameter);
        WorkflowService.WorkflowUpdate();
    }

    private void AddParameter()
    {
        InputParameter parameter = new InputParameter();
        parameter.Name = $"param{Input.Parameters.Count + 1}";
        Input.Parameters.Insert(0, parameter);
    }
    private void DeleteInput()
    {
        WorkflowService.Inputs.Remove(Input);
        WorkflowService.InputUpdate();
    }
}
